//
//  DestinationsResultPushTableViewCell.m
//  TripGo
//
//  Created by dllo on 15/7/20.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DestinationsResultPushTableViewCell.h"
#import "Notes.h"
#import "Trip.h"
#import "AttractionContents.h"
#import "UIImageView+WebCache.h"
static CGFloat width = 0;
static NSInteger i = 0;
@implementation DestinationsResultPushTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.labelText = [[UILabel alloc] init];
        self.labelText.numberOfLines = 0;
        [self.contentView addSubview:self.labelText];
        [_labelText release];
        
        self.scrollView = [[UIScrollView alloc] init];
        [self.contentView addSubview:self.scrollView];
        [_scrollView release];
        
        self.labelTime = [[UILabel alloc] init];
        self.labelTime.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.labelTime];
        [_labelTime release];
    }
    return self;
}

- (void)setAtt:(AttractionContents *)att
{
    if (_att != att) {
        [_att release];
        _att = [att retain];
    }
    self.labelText.text = att.attraction_contents_description;
    self.labelText.frame = CGRectMake(5, 5, [[UIScreen mainScreen] bounds].size.width - 10, [[self class] heightWithText:self.labelText.text]);
    NSArray *arr = att.attraction_contents_notes;
    NSDictionary *dic = att.attraction_contents_trip;
    
    for (UIImageView *imgView in self.scrollView.subviews) {
        [imgView removeFromSuperview];
    }
    Trip *t = [[Trip alloc] initWithDic:dic];
    for (NSDictionary *dic in arr) {
        Notes *n = [[Notes alloc] initWithDic:dic];
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0 + width + i * 5, 0, 150 * n.width.floatValue / n.height.floatValue, 150)];
        i++;
        width += img.frame.size.width;
        [img setImageWithURL:[NSURL URLWithString:n.photo_url] placeholderImage:[UIImage imageNamed:@"place_holder.jpg"]];
        [self.scrollView addSubview:img];
        [img release];
    }
    self.scrollView.frame = CGRectMake(0, self.labelText.frame.size.height + 10, [[UIScreen mainScreen] bounds].size.width, 150);
    self.scrollView.contentSize = CGSizeMake(width, 150);
    self.labelTime.text = [NSString stringWithFormat:@"%@--%@", t.start_date, t.end_date];
    self.labelTime.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width - 180, self.labelText.frame.size.height + self.scrollView.frame.size.height + 10, 180, 25);
    i = 0;
    width = 0;
}
+ (CGFloat)heightWithText:(NSString *)text
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGSize size = CGSizeMake([[UIScreen mainScreen] bounds].size.width - 10, 3000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回高度
    return rect.size.height;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
